package com.echatim.controller;


import com.annotation.Permission;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.broker.base.protocol.response.Resp;
import com.commom.PermissionConst;
import com.commom.Topic;
import com.echatim.dto.RoomDetailListDTO;
import com.echatim.dto.RoomGetDTO;
import com.echatim.dto.RoomListDTO;
import com.echatim.form.*;
import com.echatim.service.app.RoomService;
import com.echatim.syslog.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 群相关业务
 */
@Api(value = "IM群", tags = "IM群")
@RestController
@RequestMapping(Topic.APP_ROOM.base_uri)
public class RoomController extends ApiController {
    @Autowired
    private RoomService roomService;

    /**
     * APP添加IM群
     */
    @ApiOperation(value = "IM群-添加群")
    @SysLog("IM群-添加群")
    @Permission(value = PermissionConst.CLIENT)
    @PostMapping("/" + Topic.APP_ROOM.METHOD.ADD)
    public Resp<Long> addRoom(@RequestBody @Valid RoomAddForm addForm) {
        return roomService.add(addForm);
    }

    /**
     * APP更新IM群
     */
    @ApiOperation(value = "IM群-更新群")
    @SysLog("IM群-更新群")
    @Permission(value = PermissionConst.CLIENT)
    @PostMapping("/" + Topic.APP_ROOM.METHOD.UPDATE)
    public Resp<String> updateRoom(@RequestBody @Valid RoomUpdateForm updateForm) {
        return roomService.updateRoom(updateForm);
    }


    /**
     * APP获取IM群
     */
    @ApiOperation(value = "IM群-获取群")
    @SysLog("IM群-获取群")
    @Permission(value = PermissionConst.CLIENT)
    @PostMapping("/" + Topic.APP_ROOM.METHOD.LIST)
    public Resp<List<RoomListDTO>> listRooms(@RequestBody @Valid RoomListForm roomListForm) {
        return roomService.listRoom(roomListForm);
    }

    /**
     * APP获取单个IM群信息
     */
    @ApiOperation(value = "IM群-获取单个群")
    @SysLog("IM群-获取单个群")
    @Permission(value = PermissionConst.CLIENT)
    @PostMapping("/" + Topic.APP_ROOM.METHOD.GET)
    public Resp<RoomGetDTO> getRoom(@RequestBody @Valid RoomGetForm getForm) {
        return roomService.getRoom(getForm);
    }

    /**
     * APP获取当前用户加入的群
     */
    @ApiOperation(value = "IM群-获取当前用户加入的群")
    @SysLog("IM群-获取当前用户加入的群")
    @Permission(value = PermissionConst.CLIENT)
    @PostMapping("/" + Topic.APP_ROOM.METHOD.LIST_JOIN)
    public Resp<List<RoomListDTO>> listRoomsJoin(@RequestBody @Valid RoomListJoinForm roomListJoinForm) {
        return roomService.listRoomJoin(roomListJoinForm);
    }

    /**
     * APP获取IM群/IM群成员
     */
    @ApiOperation(value = "IM群-获取IM群/IM群成员")
    @SysLog("IM群-获取IM群/IM群成员")
    @Permission(value = PermissionConst.CLIENT)
    @PostMapping("/" + Topic.APP_ROOM.METHOD.LIST_MEMBER)
    public Resp<List<RoomDetailListDTO>> listRoomDetail(@RequestBody @Valid RoomListForm roomListForm) {
        return roomService.listRoomDetail(roomListForm);
    }


    /**
     * APP IM群添加成员
     */
    @ApiOperation(value = "IM群-添加成员")
    @SysLog("IM群-添加成员")
    @Permission(value = PermissionConst.CLIENT)
    @PostMapping("/" + Topic.APP_ROOM.METHOD.ADD_USER)
    public Resp<String> groupAddUser(@RequestBody @Valid RoomAddUserForm addUserForm) {
        return roomService.addRoomUser(addUserForm);
    }

    /**
     * APP IM群移除成员
     */
    @ApiOperation(value = "IM群-移除成员")
    @SysLog("IM群-移除成员")
    @Permission(value = PermissionConst.CLIENT)
    @PostMapping("/" + Topic.APP_ROOM.METHOD.DEL_USER)
    public Resp<String> groupDelUser(@RequestBody @Valid RoomDelUserForm delUserForm) {
        return roomService.delRoomUser(delUserForm);
    }

    /**
     * APP IM 删除群/解散群
     */
    @ApiOperation(value = "IM群-删除群/解散群")
    @SysLog("IM群-删除群/解散群")
    @Permission(value = PermissionConst.CLIENT)
    @PostMapping("/" + Topic.APP_ROOM.METHOD.DEL)
    public Resp<String> groupDel(@RequestBody @Valid RoomDelForm delForm) {
        return roomService.delRoom(delForm);
    }

    /**
     * APP 搜索群(通过name)
     */
    @ApiOperation(value = "IM群-搜索群(通过name)")
    @SysLog("IM群-搜索群(通过name)")
    @Permission(value = PermissionConst.CLIENT)
    @PostMapping("/" + Topic.APP_ROOM.METHOD.SEARCH)
    public Resp<List<RoomListDTO>> roomSearch(@RequestBody @Valid RoomSearchForm form) {
        return roomService.roomSearch(form);
    }

    /**
     * APP 加入群
     */
    @ApiOperation(value = "IM群-加入群")
    @SysLog("IM群-加入群")
    @Permission(value = PermissionConst.CLIENT)
    @PostMapping("/" + Topic.APP_ROOM.METHOD.JOIN)
    public Resp<Void> roomJoin(@RequestBody @Valid RoomJoinForm form) {
        return roomService.roomJoin(form);
    }

    /**
     * APP 退出群
     */
    @ApiOperation(value = "IM群-退出群")
    @SysLog("IM群-退出群")
    @Permission(value = PermissionConst.CLIENT)
    @PostMapping("/" + Topic.APP_ROOM.METHOD.EXIT)
    public Resp<Void> roomExit(@RequestBody @Valid RoomExitUserForm form) {
        return roomService.exitRoomUser(form);
    }
}
